package com.cms.sync.action;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import com.action.DefaultAction;
import com.cms.manager.CpInfoManager;
import com.cms.model.AppInfo;
import com.cms.model.Channel;
import com.cms.model.CpInfo;
import com.cms.model.ImportCpData;
import com.cms.service.AppInfoService;
import com.cms.service.AppInfoServiceImpl;
import com.cms.service.ChannelService;
import com.cms.service.ChannelServiceImpl;
import com.cms.sync.model.ImportCpSyncData;
import com.cms.sync.service.SyncImportDataService;
import com.cms.sync.service.SyncImportDataServiceImpl;
import com.util.Util;

public class CpDataQueryAction extends DefaultAction {
	String startdate;
	String enddate; 
	int cpid;
	String appid;
	int channelid;
	String bd;
	String goodsname;
	String operator;
	String source;
	String province;
	String dimension;
	public CpDataQueryAction()
	{}
	List<CpInfo> cplist;
	List<AppInfo> applist;
	List<AppInfo> applist1;
	List<Channel> channellist;
	ChannelService channelservice = new ChannelServiceImpl();
	AppInfoService appservice = new AppInfoServiceImpl();
	List<ImportCpSyncData> list;
	SyncImportDataService service = new SyncImportDataServiceImpl();
	
	String type;
	float totalfee;
	public String query()
	{
		cplist = CpInfoManager.getInstance().getCpInfos();
		if(cpid>0)
		{
			applist = appservice.queryAppInfo(cpid, -1, -1, "");
		}
		channellist = channelservice.getChannels();
		if(startdate==null || "".equals(startdate))
		{
			startdate = Util.dateToString(new Date(), "yyyy-MM")+"-01";
		}
		if(enddate==null || "".equals(enddate))
		{
			enddate = Util.dateToString(new Date(), "yyyy-MM-dd");
		}
		
		if("query".equals(type))
		{
			applist1 = appservice.getAppInfos();
			list = service.queryImportCpSyncDataByContent(startdate, enddate, cpid, appid, 
					channelid, bd, goodsname, operator, source, province, dimension);
			totalfee = 0;
			if(list!=null)
			{
				for(int i = 0;i<list.size();i++)
				{
					totalfee += list.get(i).getFee();
				}
			}
		}
		return SUCCESS;
	}
	public String formatfee(String fee)
	{
		if(fee.endsWith(".0"))
		{
			fee = fee.substring(0,(fee.length()-2));
		}
		return fee;
	}
	public String computearpu(float fee,int activeuser)
	{
		String arpu = "--";
		if(activeuser >0 && fee>0)
		{
			float f = fee/activeuser;
			DecimalFormat format = new DecimalFormat("#.00");
	        arpu = format.format(f);
		}
		return arpu;
	}
	public String changecp()
	{
		applist = appservice.queryAppInfo(cpid, -1, -1, "");
		return SUCCESS;
	}
	public String getchannelname(int channelid)
	{
		String channelname = "未知";
		channellist = channelservice.getChannels();
		for(int i =0;i<channellist.size();i++)
		{
			if(channellist.get(i).getId()==channelid)
			{
				return channellist.get(i).getName();
			}
		}
		return channelname;
	}

	public String getStartdate() {
		return startdate;
	}

	public void setStartdate(String startdate) {
		this.startdate = startdate;
	}

	public String getEnddate() {
		return enddate;
	}

	public void setEnddate(String enddate) {
		this.enddate = enddate;
	}

	public int getCpid() {
		return cpid;
	}

	public void setCpid(int cpid) {
		this.cpid = cpid;
	}
	
	
	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public int getChannelid() {
		return channelid;
	}

	public void setChannelid(int channelid) {
		this.channelid = channelid;
	}

	public String getGoodsname() {
		return goodsname;
	}

	public void setGoodsname(String goodsname) {
		this.goodsname = goodsname;
	}

	public String getOperator() {
		return operator;
	}

	public void setOperator(String operator) {
		this.operator = operator;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getDimension() {
		return dimension;
	}

	public void setDimension(String dimension) {
		this.dimension = dimension;
	}

	public List<CpInfo> getCplist() {
		return cplist;
	}

	public void setCplist(List<CpInfo> cplist) {
		this.cplist = cplist;
	}

	public List<AppInfo> getApplist() {
		return applist;
	}

	public void setApplist(List<AppInfo> applist) {
		this.applist = applist;
	}

	public List<Channel> getChannellist() {
		return channellist;
	}

	public void setChannellist(List<Channel> channellist) {
		this.channellist = channellist;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public List<ImportCpSyncData> getList() {
		return list;
	}
	public void setList(List<ImportCpSyncData> list) {
		this.list = list;
	}
	public float getTotalfee() {
		return totalfee;
	}
	public void setTotalfee(float totalfee) {
		this.totalfee = totalfee;
	}
	public List<AppInfo> getApplist1() {
		return applist1;
	}
	public void setApplist1(List<AppInfo> applist1) {
		this.applist1 = applist1;
	}
	public String getBd() {
		return bd;
	}
	public void setBd(String bd) {
		this.bd = bd;
	}
	
	
}
